import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: "/layout",
    component: () => import("../views/layout.vue"),
    children: [
      {
      path: "/index",
      component: () => import("../views/index.vue")
    },
    {
      path: "/menu",
      component: () => import("../views/menu.vue")
    }
  ]
  },
  {
    path: "/login",
    component: () => import("../views/login.vue"),
  }

]

const router = new VueRouter({
  routes
})

router.beforeEach(function(to,from,next){
  //设置白名单
  const no_login_list = ['/login']
  if(no_login_list.indexOf(to.path) === -1){
    //获取localstorage当中的token，校验是否存在
    var tokenString = localStorage.getItem("login_data")||'{}'
    var token = JSON.parse(tokenString).token
    if(token){
      next()
    }else{
      next("/login")
    }
  }else{
    next()
  }
})

export default router